package fun.joney;

import fun.joney.controller.First;
import fun.joney.controller.ProcessMask;
import fun.joney.controller.Second;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends Application {

    private Stage stage;
    private Stage dialogStage;
    private final double DEFAULT_PRE_WIDTH = 600.0;
    private final double DEFAULT_PRE_HEIGHT = 500.0;
    @Override
    public void start(Stage primaryStage) throws Exception{
        stage = primaryStage;
        stage.setMinWidth(DEFAULT_PRE_WIDTH);
        stage.setMinHeight(DEFAULT_PRE_HEIGHT);
        stage.setTitle("Hello JavaFX");
/*        Parent root = loader.load(Resources.getResource("first.fxml"));
        First first = (First)loader.getController();
        first.setApp(this);*/
        //Parent root = FXMLLoader.load(getClass().getResource("first.fxml"));
        gotoFirst();
        stage.show();

    }


    public static void main(String[] args) {
        launch(args);
        //Application.launch(Main.class, (java.lang.String[])null);
    }

    public void gotoSecond() {
        try {
            Second second = (Second)replaceSceneContent("/second.fxml");
            second.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotoFirst() {
        try {
            First first = (First) replaceSceneContent("/first.fxml");
            first.setApp(this);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void showMask(final Task<?> task) {
        try {
            Scene firstScene = stage.getScene();

            if(dialogStage != null){
                double screenX = firstScene.getWindow().getX() + firstScene.getX() + firstScene.getWindow().getWidth()/2 - dialogStage.getScene().getWidth()/2;
                double screenY = firstScene.getWindow().getY() + firstScene.getY() + firstScene.getWindow().getHeight()/2 - dialogStage.getScene().getHeight()/2 - 10;
                dialogStage.setX(screenX);
                dialogStage.setY(screenY);
                dialogStage.show();
            }else{
                dialogStage = new Stage();
                String fxml = "/processMask.fxml";
                FXMLLoader loader = new FXMLLoader();
                InputStream in = this.getClass().getResourceAsStream(fxml);
                loader.setBuilderFactory(new JavaFXBuilderFactory());
                loader.setLocation(this.getClass().getResource(fxml));
                VBox page;
                try {
                    page = (VBox) loader.load(in);
                } finally {
                    in.close();
                }

                page.setBackground(Background.EMPTY); //窗口背景透明
                Scene scene = new Scene(page);
                scene.setFill(null);
                double screenX = firstScene.getWindow().getX() + firstScene.getX() + firstScene.getWindow().getWidth()/2 - page.getPrefWidth()/2;
                double screenY = firstScene.getWindow().getY() + firstScene.getY() + firstScene.getWindow().getHeight()/2 - page.getPrefHeight()/2 - 10;
                dialogStage.setX(screenX);
                dialogStage.setY(screenY);
                // 窗口父子关系
                dialogStage.initOwner(stage);
                dialogStage.initStyle(StageStyle.TRANSPARENT);
                dialogStage.initModality(Modality.APPLICATION_MODAL);
                dialogStage.setScene(scene);
                dialogStage.show();
                ProcessMask mask = loader.getController();
                mask.setApp(this);
            }

            Thread inner = new Thread(task);
            inner.start();
            task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
                public void handle(WorkerStateEvent event) {
                    hideMask();
                }
            });

        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void hideMask(){
        if(dialogStage != null){
            dialogStage.close();
        }
    }
    private Node replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = this.getClass().getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(this.getClass().getResource(fxml));
        Pane page;
        try {
            page = (Pane) loader.load(in);
        } finally {
            in.close();
        }

        double stageWidth = stage.getWidth();
        if (!Double.isNaN(stageWidth)) {
            stageWidth -= (stage.getWidth() - stage.getScene().getWidth());
        }

        double stageHeight = stage.getHeight();
        if (!Double.isNaN(stageHeight)) {
            stageHeight -= (stage.getHeight() - stage.getScene().getHeight());
        }

        Scene scene = new Scene(page);
        if (!Double.isNaN(stageWidth)) {
            page.setPrefWidth(stageWidth);
        }
        if (!Double.isNaN(stageHeight)) {
            page.setPrefHeight(stageHeight);
        }

        stage.setScene(scene);
        stage.sizeToScene();
       // stage.setAlwaysOnTop(true);  //窗口总在最前
        return (Node) loader.getController();
    }

    public Stage getStage(){
        return this.stage;
    }
}
